// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
const WaveRobAnimation = "waverob"
const WaitFishAnimation = "waitfish"
const BackRobAnimation = "backrobnothing"
const WarnAnimation = "warn"
@ccclass
export default class CatController extends cc.Component {

    @property(cc.Node)
    rod:cc.Node = null

    @property(cc.Node)
    shockMark: cc.Node = null

    @property(cc.Slider)
    toughBar:cc.Slider = null
    // LIFE-CYCLE CALLBACKS:
    private catAnimation:cc.Animation
    private shockAnimation:cc.Animation
    // onLoad () {}

    private toughness:number

    
    start () {
        this.catAnimation = this.getComponent(cc.Animation)
        this.shockAnimation = this.shockMark.getComponent(cc.Animation)
        this.hideShockMark()
    }
    
    private showShockMark():void{
        this.shockMark.active = true
        this.shockAnimation.play("flashmark",0)
    }
    private hideShockMark():void{
        this.shockMark.active = false
        this.shockAnimation.stop("flashmark")
    }
    private showRod():void
    {
        this.rod.active = true
    }
    private hideRod():void
    {
        this.rod.active = false
    }

    private showToughBar():void{
        this.toughBar.node.active = true
    }

    private hideToughBar():void{
        this.toughBar.node.active = false
    }

    public PlayThrowingAnimation(){
        this.PlayAnimation(WaveRobAnimation)
    }

    public PlayWaitAnimation(){
        this.PlayAnimation(WaitFishAnimation)
    }

    public PlayBackRobAnimation(){
        this.PlayAnimation(BackRobAnimation)
    }

    public PlayAnimation(name:string){
        switch(name){
            case "idle":
                this.hideShockMark()
                this.hideRod()
                this.hideToughBar()
                this.toughness = 0
                this.toughBar.progress = 0
                this.node.color = cc.color(255,255,255,255)
                break
            case "prepare":
                this.showRod()
                break;
            case WaveRobAnimation:
                this.showRod()
                this.catAnimation.play(name)
                break;
            case WaitFishAnimation:
                this.catAnimation.play(name)
                break;
            case WarnAnimation:
                this.showShockMark()
                break;
                // this.catAnimation.play(name)
            case "fighting":
                this.showToughBar()
                break;
            case BackRobAnimation:
                this.hideShockMark()
                this.catAnimation.play(name)
                break;
            default:
                console.log("不支持的动画名=",name)
        }
        
    }
    
    public IsAnimationEnd(name:string):boolean{
        let status = this.catAnimation.getAnimationState(name)
        return status.isPlaying === false
    }

    public AddToughNess(num:number){
        this.toughness += num
        this.toughBar.progress = this.toughness/100
    }

    public IsRobBroken(){
        return this.toughness >= 100
    }

    public GetBaitNode():cc.Node{
        let baitNode = cc.find("floatitem/bait",this.rod)
        return baitNode
    }

    public GetBaitWorldBox(): cc.Rect {
        let baitNode = cc.find("floatitem/bait", this.rod)
        return baitNode.getBoundingBoxToWorld()
    }
//-----------------动画事件通知-----------------
    //收杆动画第一帧调用
    FishOnHook():void{
       
    }

    //收杆动画,和鱼对抗赢了
    FishLost():void{
        
    }

    //收杆动画,最后一帧获得鱼
    GetFish():void{
        
    }

    //收杆什么都没获得
    BackRobNothing():void{

    }

    //抛竿结束等待鱼上钩
    StartWaiting():void{
        console.log("抛竿结束等待鱼上钩")
    }
}
